LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-01-2005, 07:24 AM   #1
zidane_tribal
Member
 
Registered: Apr 2005
Location: chained to my console.
Distribution: LFS 6.1
Posts: 143

Rep: Reputation: 18
making select show its menu in a bash script?


another minor problem with select, and one i cant seem to find an answer for.

i have a little menu bash script, that i have listed below, thats using subroutines to generate some menu's. it actually works very well, with one minor problem, when the dmenu subroutine returns to the mainmenu subroutine, all i have displayed is the #? prompt, it doesnt show the mainmenu menu choices. is there a way to "force" select to re-display its menu?

ive given an example of the output below the script, i think the example explains whats happenning better than i can with words.

the script.....


Code:
#!/bin/bash


#####
#
# the menu
#
#####

#mainmenu subroutine

function mainmenu {

select menusel in "perform backup" "HS server administration" "DU server administration" "Multi server administration" "Shell administration" "" "Logout" "command line" "HELP!" ; do

	if [ "$menusel" = "perform backup" ] ; then
		echo "you chose option 1"
	fi
	
	if [ "$menusel" = "HS server administration" ] ; then
		echo "you chose option 2"
		dtype=hs
		dmenu
	fi
	
	if [ "$menusel" = "DU server administration" ] ; then
		echo "you chose option 3"
		dtype=du
		dmenu
	fi
	
	if [ "$menusel" = "Multi server administration" ] ; then
		echo "you chose option 4"
		dtype=multi
		dmenu
	fi
	
	if [ "$menusel" = "Shell administration" ] ; then
		echo "you chose option 5"
	fi
	
	if [ "$menusel" = "Logout" ] ; then
		echo "you chose option 8"
	fi
	
	if [ "$menusel" = "command line" ] ; then
		echo "you chose option 9"
		return
	fi
	
	if [ "$menusel" = "HELP!" ] ; then
		echo "i wish!"
	fi

done


}



#dmenu subroutine

function dmenu {

select dmenusel in "change password" "cycle server" "go back"; do

	if [ "$dmenusel" = "change password" ] ; then
		echo "changing password for $dtype"
	fi
	
	if [ "$dmenusel" = "cycle server" ] ; then
		echo "cycling server for $dtype"
	fi
	
	if [ "$dmenusel" = "go back" ] ; then
		return
	fi
	
done

}



mainmenu

ok, thats the script...

this is the output. i have my keyboard entry in bold, to help distinguish it from the script output...




Code:
[zidane@bluemist menu]$ ./dmenu
1) perform backup               6)
2) HS server administration     7) Logout
3) DU server administration     8) command line
4) Multi server administration  9) HELP!
5) Shell administration
#? 2
you chose option 2
1) change password
2) cycle server
3) go back
#? 3      <-----  here, it returns to the mainmenu routine
#? 8      <-----  and here, i select from the main menu, but no "menu text" is shown
you chose option 8
[zidane@bluemist menu]$
wht i am trying to do, is have the main menu text shown where i have indicated, but i cant seem to work out how to "force" select to display its menu.

does anyone know how i can make select show its menu text when the submenu subroutine returns? ive been fiddling with this all day yesterday, and most of the morning, and i just cant seem to figure it out.

any help would be much appreciated.

thanks in advance

`Zidane
 
Old 05-01-2005, 08:31 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
I think that you either want to use the tput command and roll your own menu, or put the select command in a loop, and end the commands with a break;.
 
Old 05-01-2005, 08:43 AM   #3
zidane_tribal
Member
 
Registered: Apr 2005
Location: chained to my console.
Distribution: LFS 6.1
Posts: 143

Original Poster
Rep: Reputation: 18
Quote:
Originally posted by jschiwal
I think that you either want to use the tput command and roll your own menu, or put the select command in a loop, and end the commands with a break;.
put the select command in a loop and end with a break? im not sure what you mean by that, could you expand a little further for me?
 
Old 05-01-2005, 09:58 AM   #4
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Re: making select show its menu in a bash script?

If you want to display the menu again, call the mainmenu function, e.g.-
Code:
if [ "$menusel" = "perform backup" ] ; then
		echo "you chose option 1"
		mainmenu
	fi
If you want to make your code more efficient, use "elif" instead of "if", otherwise your script will iterate through all the options even if the first one matches. Better still, use:-
Code:
case $menusel in
	"perform backup")
		echo "you chose option 1"
		mainmenu ;;
	
	"HS server administration")
		echo "you chose option 2"
		dtype=hs
		dmenu ;;
	
	"DU server administration")
		echo "you chose option 3"
		dtype=du
		dmenu ;;
	
	"Multi server administration")
		echo "you chose option 4"
		dtype=multi
		dmenu ;;
	
	"Shell administration")
		echo "you chose option 5"
		mainmenu ;;
	
	"Logout")
		echo "you chose option 8"
 		mainmenu;;
	
	"command line")
		echo "you chose option 9"
		return ;;
	
	"HELP!")
		echo "i wish!"
		mainmenu ;;
esac
Saves on typing
 
Old 05-01-2005, 10:52 AM   #5
zidane_tribal
Member
 
Registered: Apr 2005
Location: chained to my console.
Distribution: LFS 6.1
Posts: 143

Original Poster
Rep: Reputation: 18
ahhh

i had considered putting the mainmenu command in there, but surely that would mean the script has multiple subroutines to back out of, with each one needing a "go back" command?

like: mainmenu -> submenu -> mainmenu -> submenu
with each of the routines needing the user to select the relevant "go back" option?

i guess the exit on the mainmenu routine would allow the user to break out of the chain without having to back up, but im a little concerned at having them stacked like that. it seems..... 'messy' (for want of a better word)

I think im going to be using the case matching thing. as you can no doubt tell, im a relative noob when it comes to shell scripts, but i think im getting the hang of it now. the case thing looks like what i was origonally looking for before i just used if.

on the whole, ive a feeling that it might just be easier to write my own menu system, rather than using select. i think im quickly discovering limitations in select that are going to make my life hard, when i can just create some menu functions and have it work perfectly.
 
Old 05-01-2005, 01:19 PM   #6
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Quote:
Originally posted by zidane_tribal
ahhh

i had considered putting the mainmenu command in there, but surely that would mean the script has multiple subroutines to back out of, with each one needing a "go back" command?
Yes, but there is a way round that.

Ignoring the second function for the moment, I think this is what you're looking for:-
Code:
#!/bin/bash


#####
#
# the menu
#
#####

#mainmenu subroutine

function mainmenu {

select menusel in "perform backup" "HS server administration" "DU server administration" "Multi server administration" "Shell administration" "" "Logout" "command line" "HELP!" "EXIT PROGRAM"; do
case $menusel in
	"perform backup")
		echo "you chose option 1" ;;
	
	"HS server administration")
		echo "you chose option 2" ;;
	
	"DU server administration")
		echo "you chose option 3" ;;
	
	"Multi server administration")
		echo "you chose option 4" ;;
	
	"Shell administration")
		echo "you chose option 5" ;;
	
	"Logout")
		echo "you chose option 8" ;;
	
	"command line")
		echo "you chose option 9" ;;
	
	"HELP!")
		echo "i wish!" ;;

	"EXIT PROGRAM")
		exit 0 ;;
esac

break

done
}

while true; do mainmenu; done
The "break" after the "case" block will make sure "select" exits after every selection, but the "while true; do" will simply call the "mainmenu" function again.

The "EXIT PROGRAM" option is there to stop the infinite loop, which you may or may not want to add.
 
Old 05-02-2005, 05:52 AM   #7
zidane_tribal
Member
 
Registered: Apr 2005
Location: chained to my console.
Distribution: LFS 6.1
Posts: 143

Original Poster
Rep: Reputation: 18
aaahhhh, now i see what you mean

ahh, yeah, that looks like its taking some serious shape there.

i think the while and case are the way to go, tbh im not sure select is what i need for this, for all the time its taken me to figure out how to bend it to my will, i could have just written a menu from scratch. whilst select is usefull for "quick and dirty" menu selections, i think something like this is gonna need me to get my hands dirty and make it myself.

so far im working on the theory of:

while var is true, do

display menu

read

case for answers

wend.

or similar, it seems a pretty solid way to go with it. much like your example, only with a man-made menu instead of select.


thanks for all the help guys, it seems like select is just not what i was looking for on this one, but it was good to see the different ways of using it that you suggested looks like its back to the drawing board on this one, but as they say, all experience is good experience.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
making a bash script file Berticus Linux - General 1 10-15-2005 11:14 PM
making a bash script student04 Linux - Software 5 01-11-2005 03:41 AM
how to get the select menu k_ravindra_babu Linux - Newbie 1 01-04-2004 05:35 AM
How logout from login-menu bash script? MikHud Linux - General 0 04-12-2002 12:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration